let server = require("express")();
let expressStatic = require("express-static")
let mysql = require("mysql")
let bodyParser = require('body-parser')
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({ extended: false }));
const port = 8080;

//获取数据库
let sql = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'student',
    timezone: "08:00"
});

sql.connect();

//后端给前端发
server.get("/studentList", (request, response) => {
    sql.query(`SELECT * FROM student`, (error, data) => {
        if (error) {
            console.log(error)
            response.send("error")
        } else {
            response.send(JSON.stringify(data))
        }
    })
})

//删除
server.get("/deleteStudent", (request, response) => {
    sql.query(`DELETE FROM student WHERE id=${request.query.id}`, (error, data) => {
        if (error) {
            console.log(error)
            response.send("error")
        } else {
            response.send("success")
        }
    })
})

//添加
server.get("/addStudent", (request, response) => {
    let p = request.query;
    sql.query(`INSERT INTO student (name,age,sex,city,date) VALUES ("${p.name}","${p.age}","${p.sex}","${p.city}","${p.date}")`, (error, data) => {
        if (error) {
            console.log(error)
            response.send("error")
        } else {
            response.send("success")
            console.log(`修改学生${p.name}操作成功`)
        }
    })
});
//更新仓库 
server.get("/editStudent", (request, response) => {
    let p = request.query;
    sql.query(`UPDATE student SET name="${p.name}",age="${p.age}",sex="${p.sex}",city="${p.city}",date="${p.date}"WHERE id="${p.id}"`, (error, data) => {
        if (error) {
            console.log(error)
            response.send("error")
        } else {
            response.send("success")
            console.log(`修改学生${p.name}操作成功`)
        }
    })
})

//登陆
server.get("/denglu", (request, response) => {
    let q = request.query;
    sql.query(`SELECT * FROM users WHERE username="${q.username}" AND password="${q.password}"`, (error, data) => {
        if (error) {
            console.log(error)
            response.send("3")
                //数据库接收失败
        } else {
            if (data.length === 0) {
                response.send("2")
                    //查不到信息
            } else {
                response.send(data)
            }
            console.log(data)
        }
    })
});
//注册
server.get("/zhuce", (request, response) => {
    let q = request.query;
    sql.query(`INSERT INTO users (name,username,password,type) VALUES ("${q.name}","${q.username}","${q.password}","${q.type}")`, (error, data) => {
        if (error) {
            console.log(error)
            response.send("error")
                //数据库接收失败
        } else {
            if (data.length === 0) {
                // response.send("2")
                //查不到信息
            } else {
                response.send(JSON.stringify(data))
            }
            console.log(data)
        }
    })
})

server.use(expressStatic(__dirname + '/student'))

server.listen(port);
console.log(`server is running at ${port}`)